package com.smartum.mpr.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.smartum.mpr.client.services.ItemsService;
import com.smartum.mpr.server.utils.ConnectionPropertyReader;
import com.smartum.mpr.server.utils.JSONHelper;
import com.smartum.mpr.shared.Category;
import com.smartum.mpr.shared.Subcategory;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 */
public class ItemsServiceImpl extends RemoteServiceServlet implements ItemsService {
    public List<Category> getCategories() throws IOException {
        final HttpServletRequest request = this.getThreadLocalRequest();
        final HttpSession session = request.getSession(false);

        final Object authToken = session.getAttribute(JSONHelper.AUTH_TOKEN);
        final JSONObject webRequestHeader = JSONHelper.getWebRequestHeader(authToken);

        final HttpURLConnection conn = JSONHelper.getHttpURLConnection(ConnectionPropertyReader.getItemsListURL());
        try {
            conn.connect();
        } catch (IOException e) {
        }

        final String response = JSONHelper.proceedRequest(webRequestHeader, conn);

        List<Category> categories = new ArrayList<Category>();
        try {
            categories = JSONHelper.proceedItemsResponse(ConnectionPropertyReader.getSubitemsListURL(), response, authToken);
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return categories;
    }

    public List<Category> addCategory(String nameCategory) throws IOException {

        final HttpServletRequest request = this.getThreadLocalRequest();
        final HttpSession session = request.getSession(false);

        final Object authToken = session.getAttribute(JSONHelper.AUTH_TOKEN);
        final JSONObject webRequestHeader = JSONHelper.getWebRequestHeader(authToken);

        JSONObject requestObject = new JSONObject();
        requestObject.put("category_name", nameCategory);

        webRequestHeader.put("request", requestObject);

        final HttpURLConnection conn = JSONHelper.getHttpURLConnection(ConnectionPropertyReader.getAddItemsListURL());
        try {
            conn.connect();
        } catch (IOException e) {
        }
        JSONHelper.proceedRequest(webRequestHeader, conn);

        return getCategories();
    }

    public List<Category> editCategory(String id, String nameCategory) throws IOException {

        final HttpServletRequest request = this.getThreadLocalRequest();
        final HttpSession session = request.getSession(false);

        final Object authToken = session.getAttribute(JSONHelper.AUTH_TOKEN);
        final JSONObject webRequestHeader = JSONHelper.getWebRequestHeader(authToken);

        JSONObject requestObject = new JSONObject();
        requestObject.put("category_name", nameCategory);
        requestObject.put("category_id", new Long(id));

        webRequestHeader.put("request", requestObject);

        final HttpURLConnection conn = JSONHelper.getHttpURLConnection(ConnectionPropertyReader.getEditItemsListURL());
        try {
            conn.connect();
        } catch (IOException e) {
        }
        JSONHelper.proceedRequest(webRequestHeader, conn);

        return getCategories();
    }

    public List<Category> deleteCategory(String id) throws IOException {
        final HttpServletRequest request = this.getThreadLocalRequest();
        final HttpSession session = request.getSession(false);

        final Object authToken = session.getAttribute(JSONHelper.AUTH_TOKEN);
        final JSONObject webRequestHeader = JSONHelper.getWebRequestHeader(authToken);

        JSONObject requestObject = new JSONObject();
        requestObject.put("category_id", new Long(id));

        webRequestHeader.put("request", requestObject);

        final HttpURLConnection conn = JSONHelper.getHttpURLConnection(ConnectionPropertyReader.getDeleteItemsListURL());
        try {
            conn.connect();
        } catch (IOException e) {
        }
        JSONHelper.proceedRequest(webRequestHeader, conn);

        return getCategories();
    }

    public List<Category> addSubCategory(String categoryId, String nameSubcategory) throws IOException {
        final HttpServletRequest request = this.getThreadLocalRequest();
        final HttpSession session = request.getSession(false);

        final Object authToken = session.getAttribute(JSONHelper.AUTH_TOKEN);
        final JSONObject webRequestHeader = JSONHelper.getWebRequestHeader(authToken);

        JSONObject requestObject = new JSONObject();
        requestObject.put("category_id", new Long(categoryId));
        requestObject.put("subcategory_name", nameSubcategory);

        webRequestHeader.put("request", requestObject);

        final HttpURLConnection conn = JSONHelper.getHttpURLConnection(ConnectionPropertyReader.getAddSubitemsListURL());
        try {
            conn.connect();
        } catch (IOException e) {
        }
        JSONHelper.proceedRequest(webRequestHeader, conn);

        return getCategories();
    }

    public List<Category> editSubCategory(String id, String nameSubcategory) throws IOException {
        final HttpServletRequest request = this.getThreadLocalRequest();
        final HttpSession session = request.getSession(false);

        final Object authToken = session.getAttribute(JSONHelper.AUTH_TOKEN);
        final JSONObject webRequestHeader = JSONHelper.getWebRequestHeader(authToken);

        JSONObject requestObject = new JSONObject();
        requestObject.put("subcategory_name", nameSubcategory);
        requestObject.put("subcategory_id", new Long(id));

        webRequestHeader.put("request", requestObject);

        final HttpURLConnection conn = JSONHelper.getHttpURLConnection(ConnectionPropertyReader.getEditSubitemsListURL());
        try {
            conn.connect();
        } catch (IOException e) {
        }
        JSONHelper.proceedRequest(webRequestHeader, conn);

        return getCategories();
    }

    public List<Category> deleteSubCategory(String id) throws IOException {
        final HttpServletRequest request = this.getThreadLocalRequest();
        final HttpSession session = request.getSession(false);

        final Object authToken = session.getAttribute(JSONHelper.AUTH_TOKEN);
        final JSONObject webRequestHeader = JSONHelper.getWebRequestHeader(authToken);

        JSONObject requestObject = new JSONObject();
        requestObject.put("subcategory_id", new Long(id));

        webRequestHeader.put("request", requestObject);

        final HttpURLConnection conn = JSONHelper.getHttpURLConnection(ConnectionPropertyReader.getDeleteSubitemsListURL());
        try {
            conn.connect();
        } catch (IOException e) {
        }
        JSONHelper.proceedRequest(webRequestHeader, conn);

        return getCategories();
    }
}
